package com.fingard.dsp.bank.directbank.boc01;

import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.dsp.bank.reqrespobj.enumeration.ENDirectRight;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component("BOC01Balance")
public class BOC01Balance extends BOC01Base implements IBalance {

    /**
     * 即时余额查询
     * @param p_req7506
     * @return
     * @throws Exception
     */
    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        //拆分境内境外报文
        Map<String,Req7506BalQry> req7506QryMap=getReq7506QryMap(p_req7506);
        //分别请求
        for(Map.Entry<String,Req7506BalQry> map:req7506QryMap.entrySet()){
            if("1".equals(map.getKey())){
                d_7506_BalQry_b2e0037(map.getValue(),retAts);//境外账户余额
            }else if("0".equals(map.getKey())){
                d_7506_BalQry_b2e0005(map.getValue(),retAts);//境内账户余额
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 境外中行
     * @param p_req7506
     * @return
     * @throws Exception
     */
    public void d_7506_BalQry_b2e0037(Req7506BalQry p_req7506,RetAts7506BalQry retAts) throws Exception {
        while (p_req7506.readRecord()){
            try {
                BOC01Header bankHeader = new BOC01Header();
                bankHeader.trncod = "b2e0037";//交易代码
                bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader2(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("trn-b2e0037-rq");
                xmlWriter.writeStartElement("b2e0037-rq");
                xmlWriter.writeStartElement("account");
                String areaCode = getBankConfig().getMapKeyByValue("areaMapping", p_req7506.readedDetail.actArea);
                areaCode = StringHelper.hasAnyChar(areaCode) ? areaCode : p_req7506.readedDetail.actArea;
                xmlWriter.writeElementString("ibknum", areaCode);
                xmlWriter.writeElementString("actacn", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //是否需要和前一次请求保持5秒的间隔
                boolean isNeedWait  = isNeedWaitFiveS();
                if(isNeedWait) {
                    if (bankFront.lastVisitTime != null) {
                        long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
                        if (timeSpan < 5000l) {
                            long sleepMils = 5000l - timeSpan;
                            this.WriteBankLogLn2("sleep(" + sleepMils + ")");
                            Thread.sleep(5000l - timeSpan);
                        }
                    }
                }
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                //测试
                //String[] tmpStrRet=new String[]{"",FileHelper.readFileText("D:\\zjbr-接口测试\\25.中行联行号下载\\境外账户查询\\rsp\\b2e0037.xml","utf-8")};

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0037-rs/status/rspcod");
                        String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0037-rs/status/rspmsg");
                        if (isActiveRetCode(tmprspcod)) {
                            while (tmpBkReader.read("/bocb2e/trans/trn-b2e0037-rs/b2e0037-rs")) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                if (tmpBkReader.readedXElement.getSingleChild("status") != null) {
                                    String tmpEachRspcod = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                                    String tmpEachRspmsg = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                                    if ("B001".equals(tmpEachRspcod)) {
                                        tmpRetDetail.respCode = "0";
                                        tmpRetDetail.respInfo=tmpEachRspmsg;
                                        tmpRetDetail.actArea = tmpBkReader.readedXElement.getSingleText("account/ibknum");
                                        tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("account/actacn");
                                        tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("account/curcde"));
                                        tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance/bokbal");
                                        tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("balance/avabal");
                                    } else {
                                        tmpRetDetail.respCode = "-1";
                                        tmpRetDetail.respInfo = tmpEachRspmsg;
                                        if (StringHelper.isNullOrEmpty(tmpRetDetail.respInfo)) {
                                            tmpRetDetail.respInfo = tmpEachRspcod;
                                        }
                                    }
                                } else {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = "status节点为空";
                                }
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            }
                        } else {
                            if(!retAts.hasWrittenRespInfo){
                                retAts.writeRespCode("-1");
                                if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                                    tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                                }
                                retAts.writeRespInfo(tmprspmsg);
                            }
                            break;
                        }
                    } else {
                        if(!retAts.hasWrittenRespInfo) {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpSbMsg.toString());
                        }
                        break;
                    }
                } else {
                    if(!retAts.hasWrittenRespInfo) {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpStrRet[1]);
                    }
                    break;
                }
            } catch (Exception e) {
                if(!retAts.hasWrittenRespInfo) {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(LogHelper.getStackTrace(e));
                }
                e.printStackTrace();
            }
        }
    }

    /**
     * 境内中行
     * @param p_req7506
     * @return
     * @throws Exception
     */
    public RetAts7506BalQry d_7506_BalQry_b2e0005(Req7506BalQry p_req7506,RetAts7506BalQry retAts) throws Exception {
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0005";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0005-rq");
            int count = 0;
            boolean isDirectRight=false;
            Map<String, String> map = new HashMap<String, String>();
            while (p_req7506.readRecord()){
                count++;
                isDirectRight = p_req7506.readedDetail.directRight.contains(ENDirectRight.CASHPOOLS.getValue()) ? true : false;
                map.put("actNum", p_req7506.readedDetail.actNum);
                map.put("actArea", p_req7506.readedDetail.actArea);
                map.put("transSeqID", p_req7506.reqHeader.transSeqID);
                xmlWriter.writeStartElement("b2e0005-rq");
                xmlWriter.writeStartElement("account");
                String areaCode = getBankConfig().getMapKeyByValue("areaMapping", p_req7506.readedDetail.actArea);
                areaCode = StringHelper.hasAnyChar(areaCode) ? areaCode : p_req7506.readedDetail.actArea;
                if(getBankConfig().getValueAsBool("setIbknumWithEmpty")){
                    xmlWriter.writeElementString("ibknum", "");
                }else{
                    xmlWriter.writeElementString("ibknum", areaCode);
                }
                xmlWriter.writeElementString("actacn", p_req7506.readedDetail.actNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();
            //是否需要和前一次请求保持5秒的间隔
            boolean isNeedWait  = isNeedWaitFiveS();
            if(isNeedWait) {
                if (bankFront.lastVisitTime != null) {
                    long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
                    if (timeSpan < 5000l) {
                        long sleepMils = 5000l - timeSpan;
                        this.WriteBankLogLn2("sleep(" + sleepMils + ")");
                        Thread.sleep(5000l - timeSpan);
                    }
                }
            }
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            //String[] tmpStrRet=new String[]{"",FileHelper.readFileText("D:\\zjbr-接口测试\\25.中行联行号下载\\境外账户查询\\rsp\\b2e0005.xml","utf-8")};

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0005-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0005-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        while (tmpBkReader.read("/bocb2e/trans/trn-b2e0005-rs/b2e0005-rs")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            if (tmpBkReader.readedXElement.getSingleChild("status") != null) {
                                String tmpEachRspcod = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspcod");
                                String tmpEachRspmsg = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspmsg");
                                if (tmpEachRspcod.equals("B001")) {
                                    tmpRetDetail.respCode = "0";
                                    if (tmpBkReader.readedXElement.getSingleChild("account") != null) {
                                        tmpRetDetail.actArea = tmpBkReader.readedXElement.getSingleChild("account").getSingleText("ibknum");
                                        tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleChild("account").getSingleText("actacn");
                                        tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleChild("account").getSingleText("curcde"));
                                    } else {
                                        tmpRetDetail.respCode = "-1";
                                        tmpRetDetail.respInfo = "无account节点";
                                    }
                                    if (tmpBkReader.readedXElement.getSingleChild("balance") != null) {
                                        tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleChild("balance").getSingleText("bokbal");
                                        tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleChild("balance").getSingleText("avabal");
                                    } else {
                                        tmpRetDetail.respCode = "-1";
                                        tmpRetDetail.respInfo = "无balance节点";
                                    }
                                } else {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = tmpEachRspmsg;
                                    if (StringHelper.isNullOrEmpty(tmpRetDetail.respInfo)) {
                                        tmpRetDetail.respInfo = tmpEachRspcod;
                                    }
                                }
                            } else {
                                tmpRetDetail.respCode = "-1";
                                tmpRetDetail.respInfo = "status节点为空";
                            }
                            //判断账号有现金池权限是，查询现金池余额接口
                            if(!p_req7506.isBatch && count == 1 && isDirectRight == true) {
                                qry7506CashPoolBal(map, tmpRetDetail);
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        if(!retAts.hasWrittenRespInfo){
                            retAts.writeRespCode("-1");
                            if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                                tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                            }
                            retAts.writeRespInfo(tmprspmsg);
                        }
                    }
                } else {
                    if(!retAts.hasWrittenRespInfo) {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                }
            } else {
                if(!retAts.hasWrittenRespInfo) {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            }
        } catch (Exception e) {
            if(!retAts.hasWrittenRespInfo) {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(LogHelper.getStackTrace(e));
            }
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 历史余额查询
     * @param p_req7505
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        if(StringHelper.hasAnyChar(p_req7505.swiftCode) && !swiftcodeFlag.equals(p_req7505.swiftCode.substring(0,6))){
            return d_7505_BalQry_b2e0039(p_req7505);
        }else {
            return d_7505_BalQry_b2e0012(p_req7505);
        }
    }

    /**
     * 境外中行
     * @param p_req7505
     * @return
     * @throws Exception
     */
    public RetAts7505HisBalQry d_7505_BalQry_b2e0039(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0039";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0039-rq");
            xmlWriter.writeStartElement("b2e0039-rq");
            xmlWriter.writeStartElement("account");
            xmlWriter.writeElementString("ibknum", p_req7505.actArea);
            xmlWriter.writeElementString("actacn", p_req7505.actNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("datescope");
            xmlWriter.writeElementString("from", p_req7505.dateFrom);
            xmlWriter.writeElementString("to", p_req7505.dateTo);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());

            //测试
            //String[] tmpRetBkStr=new String[]{"",FileHelper.readFileText("D:\\zjbr-接口测试\\25.中行联行号下载\\境外账户查询\\rsp\\b2e0039.xml","utf-8")};

            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0039-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0039-rs/status/rspmsg");
                    if(isActiveRetCode(tmprspcod)){
                        while (tmpBkReader.read("/bocb2e/trans/trn-b2e0039-rs/b2e0039-rs")){
                            if (tmpBkReader.readedXElement.getSingleChild("status") != null) {
                                String tmpEachRspcod = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                                if ("B001".equals(tmpEachRspcod)) {
                                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                                    if (tmpBkReader.readedXElement.getSingleChild("balance") != null) {
                                        tmpRetDetail.balDate = tmpBkReader.readedXElement.getSingleText("balance/baldat").replaceAll("/","-");
                                        tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("balance/todbal");
                                    }
                                    retAts.writeRetDetail(tmpRetDetail);
                                }else {
                                    tmpRespCode = "-1";
                                    tmpRespInfo = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                                }
                            }
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 境内中行
     * @param p_req7505
     * @return
     * @throws Exception
     */
    public RetAts7505HisBalQry d_7505_BalQry_b2e0012(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0012";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0012-rq");

            xmlWriter.writeStartElement("b2e0012-rq");
            xmlWriter.writeStartElement("account");
            xmlWriter.writeElementString("ibknum", "");
            xmlWriter.writeElementString("actacn", p_req7505.actNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("datescope");
            xmlWriter.writeElementString("from", p_req7505.dateFrom);
            xmlWriter.writeElementString("to", p_req7505.dateTo);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();

            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0012-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0012-rs/status/rspmsg");
                    if (isActiveRetCode(tmprspcod)) {
                        while (tmpBkReader.read("/bocb2e/trans/trn-b2e0012-rs/b2e0012-rs")) {
                            if (tmpBkReader.readedXElement.getSingleChild("status") != null) {
                                String tmpEachRspcod = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspcod");
                                if (tmpEachRspcod.equals("B001")) {
                                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                                    String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("baldat");
                                    if (tmpETYDAT.length() == 8) {
                                        tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
                                    } else {
                                        tmpRetDetail.balDate = tmpETYDAT;
                                    }
                                    tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleChild("balance").getSingleText("avabal");
                                    retAts.writeRetDetail(tmpRetDetail);
                                }else {
                                    tmpRespCode = "-1";
                                    tmpRespInfo = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspmsg");
                                }
                            }
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 现金池余额查询
     */
    private void qry7506CashPoolBal(Map<String, String> map, RetAtsBalDetail tmpRetDetail) {
        try {
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0050";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0050-rq");
            xmlWriter.writeStartElement("b2e0050-rq");
            xmlWriter.writeStartElement("account");
            String areaCode = getBankConfig().getMapKeyByValue("areaMapping", map.get("actArea"));
            areaCode = StringHelper.hasAnyChar(areaCode) ? areaCode : map.get("actArea");
            xmlWriter.writeElementString("ibknum", areaCode);
            xmlWriter.writeElementString("actacn", map.get("actNum"));
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            //region 是否需要和前一次请求保持5秒的间隔
//            boolean isNeedWait = isNeedWaitFiveS();
//            if (isNeedWait) {
//                if (bankFront.lastVisitTime != null) {
//                    long timeSpan = -bankFront.lastVisitTime.getTime() + new Date().getTime();
//                    if (timeSpan < 5000l) {
//                        long sleepMils = 5000l - timeSpan;
//                        this.WriteBankLogLn2("sleep(" + sleepMils + ")");
//                        Thread.sleep(5000l - timeSpan);
//                    }
//                }
//            }
            //endregion

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();

                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0050-rs/status/rspcod");
                    if (isActiveRetCode(tmprspcod)) {
                        while (tmpBkReader.read("/bocb2e/trans/trn-b2e0050-rs/b2e0050-rs")) {
                            if (tmpBkReader.readedXElement.getSingleChild("status") != null) {
                                String tmpEachRspcod = tmpBkReader.readedXElement.getSingleChild("status").getSingleText("rspcod");
                                if (tmpEachRspcod.equals("B001")) {
                                    tmpRetDetail.respCode = "0";
                                    if (tmpBkReader.readedXElement.getSingleChild("balance") != null) {
                                        tmpRetDetail.cashPoolBal = tmpBkReader.readedXElement.getSingleChild("balance").getSingleText("bokbal");
                                        tmpRetDetail.cashPoolAvailBal = tmpBkReader.readedXElement.getSingleChild("balance").getSingleText("useablequota");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            WriteBankLogLn("现金池余额查询异常:" +e.getMessage());
        }
    }

}
